<?php
namespace Borrow\Controller;
use Common\Controller\AdminbaseController;

class BorrowexportadminController extends AdminbaseController{
		function _initialize() {
			parent::_initialize();
							
		}
	function  index(){
		//vendor("PHPExcel.PHPExcel");//此处是测试第三方类库是否导入成功，在simplewind/Core/Library/vendor/phpexcel/phpexcel.php里写入一句"echo 我是第三方类库",看这里能不能输出，就知道了。
		$this->display();
	}
   public function exportExcel($expTitle,$expCellName,$expTableData){
		$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
		$fileName = 'Tgzc'.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
		$cellNum = count($expCellName);
		$dataNum = count($expTableData);
		ini_set('max_execution_time', '3000');                                        // 就是这里需要手动改变下php.ini中的运存的大小和超时时间的长短
        ini_set('memory_limit', '1024M');                                             // 就是这里需要手动改变下php.ini中的运存的大小和超时时间的长短

		vendor("PHPExcel.PHPExcel");		
		$objPHPExcel = new \PHPExcel();//注意此处必须有\，否则实例化的时候找不到该类；		
		
					
		$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
		
		$objPHPExcel->getActiveSheet()->getStyle('C')->getNumberFormat()
					->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);  //注意要加\，不然提示找不到类；
		$objPHPExcel->getActiveSheet()->getStyle('E')->getNumberFormat()
					->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);  //注意要加\，不然提示找不到类；
		$objPHPExcel->getActiveSheet()->getStyle('G')->getNumberFormat()
					->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);  //注意要加\，不然提示找不到类；


		$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
		// $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
		
		
		for($i=0;$i<$cellNum;$i++){
			$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
		}
		// Miscellaneous glyphs, UTF-8
		for($i=0;$i<$dataNum;$i++){
			for($j=0;$j<$cellNum;$j++){
				$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
			}
		}
		ob_end_clean();//清除缓冲区,避免乱码
		header('pragma:public');
		header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
		header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//注意此处必须有\，否则实例化的时候找不到该类；
		$objWriter->save('php://output');
		exit;
	}

	/**
	 *
	 * 导出Excel
	 */
	function expBorrow(){//导出Excel
		$xlsName  = "Tgzc";
		$xlsCell  = array(
		array('id','编号'),
		array('xys','信用社'),
		array('khh','客户号'),
		array('name','客户名称'),
		array('zhdh','贷款账号'),
		array('yt','用途'),		
		array('jjh','借据号'),
		array('ffrq','发放日期'),
		array('dqrq','到期日期'),
		array('fslx','发放类型'),
		array('ffje','贷款金额'),
		array('zhye','贷款余额'),
		array('dbfs','担保方式'),
		array('zxll','执行利率'),	
		array('zhjxr','最后结息日'),
		array('zmwjfl','账面五级分类'),
		array('sjwjfl','实际五级分类'),
		array('khjl','客户经理')				
		);
		$xlsModel = M('Borrow');

		$xlsData  = $xlsModel->Field('id,xys,khh,name,zhdh,yt,jjh,ffrq,dqrq,fslx,ffje,zhye,dbfs,zxll,zhjxr,zmwjfl,sjwjfl,khjl')->select();		
		$this->exportExcel($xlsName,$xlsCell,$xlsData);
			
	}	
}